layui.use(['rate'], function () {
    var rate = layui.rate,
        $ = layui.jquery;
    $.ajax({
        url: "/home/getProjectScoreAvgByProjectId?id=" + GetQueryString("id"),
        success: function (data) {
            $("#score_avg").text(data.data);
            rate.render({
                elem: '#score'
                , value: data.data
                , theme: '#009688'
                , half: true //开启半星
                , choose: function (value) {
                    layer.prompt({
                        formType: 2,
                        value: '',
                        title: '请填写评价内容',
                        area: ['400px', '250px'] //自定义文本域宽高
                    }, function (inputV, index, elem) {
                        // alert(value); //得到value
                        $.ajax({
                            url: "/home/addProjectScore?project.projectId=" + GetQueryString("id") + "&score=" + value + "&scoreContent=" + inputV,
                            success: function (data) {
                                alert(data.msg);
                                getScoreList(1,5);
                            }
                        });
                        layer.close(index);

                    });

                }
            });
        }
    });

});


//分页显示评分
getScoreList(1, 5);

function getScoreList(page, limit) {
    $("#score_list").html("");
    //获取评分
    $.ajax({
        url: "/home/findAllScoreByProject",
        data: {"projectId": GetQueryString("id"), "page": page, "limit": limit},
        success: function (data) {
            console.log(data);
            var html = "";
            $.each(data.data, function (k, obj) {
                var score = obj.score;
                html += "<dl class='ask_list'>\n" +
                    "     <dt>" + obj.homeUser.username + "<em>" + obj.createTime + "</em></dt>\n" +
                    "        <dd class='ask_question bk'> " + obj.scoreContent + "\n" +
                    "        <div class='ask_question_stars'>\n" +
                    "                <div class='pro_rating_action'><em>评分：</em>\n" +
                    "                   <div class='evaluate_score'><span class='info_stars' style='width:" + parseFloat(score) / 5 * 100 + "px'></span></div>\n" +
                    "            </div>\n" +
                    "        </div>\n" +
                    "        </dd>\n" +
                    "  </dl>"
            });
            if (page==1&&data.data.length==0){
                return;
            }
            $("#score_list").prepend(html);
            var pre = page;
            var next = page;
            if (page > 1) {
                pre = page - 1;
            }
            if (data.data.length == 5) {
                next = page + 1;
            }
            if (data.data.length == 0&&page!=1) {
                getScoreList(page - 1, 5);
            } else {
                var p_html = "    <div class='pro_ask_page'>\n" +
                    "       <a href='javascript:getScoreList(" + pre + ",5);'>上一页</a>\n" +
                    "       <a href='javascript:getScoreList(" + next + ",5);'>下一页</a>\n" +
                    "      </div>";
                if (page==1&&data.data.length<5){

                }else
                    $("#score_list").append(p_html);
            }
        }
    });
}


//咨询功能
$("#doit").click(function () {
    var content = $("#content_ask").val();
    $.ajax({
        url: "/home/addComments",
        data: {"commentsContent": content, "project.projectId": GetQueryString("id")},
        success: function (data) {
            console.log(data);
            $("#content_ask").val("");
            alert(data.msg);
            getFaqList(1,5);
        }
    });
});



//获取评论列表
getFaqList(1,5);

function getFaqList(page, limit) {
    $("#faq_list").html("");
    //获取评分
    $.ajax({
        url: "/home/findAllByProject",
        data: {"projectId": GetQueryString("id"), "page": page, "limit": limit},
        success: function (data) {
            console.log(data);
            var html = "";
            $.each(data.data, function (k, obj) {
                html = html + "     <dl class='ask_list'>\n" +
                    "                    <dt>" + obj.homeUser.username + "<em>" + obj.createTime + "</em></dt>\n" +
                    "                    <dd class='ask_question bk'>" + obj.commentsContent + "</dd>\n" +
                    "                    <dd class='ask_answer'>\n" +
                    "                        <div class='ask_answer_info bk'>项目发布者还没有给\n" +
                    "                            <a href='javascript:;' onclick='open_answer(this);' " +
                    "                                   class='zc_answer' qid='" + obj.commentsId + "'>回复</a>\n" +
                    "                        </div>\n" +
                    "                        <p class='ask_answer_author'></p><span class='ask_answer_arrow'></span></dd>\n" +
                    "                </dl>";
            });
            if (page==1&&data.data.length==0){
                return;
            }
            $("#faq_list").prepend(html);
            var pre = page;
            var next = page;
            if (page > 1) {
                pre = page - 1;
            }
            if (data.data.length == 5) {
                next = page + 1;
            }
            if (data.data.length == 0&&page!=1) {
                getFaqList(page - 1, 5);
            } else {

                var page_html = "<div class='pro_ask_page'>\n" +
                    "     <a href='javascript:getFaqList(" + pre + ",5);'>上一页</a>\n" +
                    "     <a href='javascript:getFaqList(" + next + ",5);'>下一页</a>\n" +
                    "</div>";
                if (page==1&&data.data.length<5){
                }else
                    $("#faq_list").append(page_html);
            }
        }
    });
}






